// pages/home/home.js

import request from "../../util/request"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    limit: 5,
    goodList: [],
    swiperList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.renderSwiper()
    this.renderGoods()
  },

  renderSwiper() {
    request({
      url: "/recommends"
    }).then(res => {
      this.setData({
        swiperList: res
      })
    })
  },
  renderGoods() {
    request({
      url: "/goods?_page=" + this.data.page + "&_limit=" + this.data.limit
    }, true).then((data, header) => {
      this.setData({
        goodList: [...this.data.goodList, ...data.list],
        total: data.total
      })
    })
  },
  handleEvent() {
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  handleChangePage(evt) {
    let id = evt.currentTarget.dataset.id
    let title = evt.currentTarget.dataset.title
    wx.navigateTo({
      url: `/pages/detali/detali?id=${id}&title=${title}`
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    setTimeout(() => {
      wx.stopPullDownRefresh({
        success: (res) => {},
      })
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.goodList.length == this.data.total) {
      return
    }
    this.setData({
      page: ++this.data.page
    })
    this.renderGoods()

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})